package cn.da.shuai.cool.system.service.service.impl;

import cn.da.shuai.cool.common.expection.CoolServiceException;
import cn.da.shuai.cool.mybatis.service.impl.CoolServiceImpl;
import cn.da.shuai.cool.system.service.mapper.CoolDictTypeMapper;
import cn.da.shuai.cool.system.model.entity.dict.CoolDictType;
import cn.da.shuai.cool.system.service.service.ICoolDictDataService;
import cn.da.shuai.cool.system.service.service.ICoolDictTypeService;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.Objects;

/**
 * 字典类型服务实现类
 */
@Service
public class CoolDictTypeServiceImpl extends CoolServiceImpl<CoolDictTypeMapper,CoolDictType> implements ICoolDictTypeService {

    @Autowired
    private ICoolDictDataService dictDataService;

    @Override
    public Page<CoolDictType> selectDictTypePage(CoolDictType dictType, Page<CoolDictType> page) {
        LambdaQueryWrapper<CoolDictType> lqw = new LambdaQueryWrapper<>();
        lqw.like(StrUtil.isNotBlank(dictType.getDictName()), CoolDictType::getDictName, dictType.getDictName())
           .eq(StrUtil.isNotBlank(dictType.getStatus()), CoolDictType::getStatus, dictType.getStatus())
           .like(StrUtil.isNotBlank(dictType.getDictType()), CoolDictType::getDictType, dictType.getDictType());
        return this.page(page, lqw);
    }

    @Override
    public List<CoolDictType> selectDictTypeList(CoolDictType dictType) {
        LambdaQueryWrapper<CoolDictType> lqw = new LambdaQueryWrapper<>();
        lqw.like(StrUtil.isNotBlank(dictType.getDictName()), CoolDictType::getDictName, dictType.getDictName())
           .eq(StrUtil.isNotBlank(dictType.getStatus()), CoolDictType::getStatus, dictType.getStatus())
           .like(StrUtil.isNotBlank(dictType.getDictType()), CoolDictType::getDictType, dictType.getDictType());
        return this.list(lqw);
    }

    @Override
    public CoolDictType selectDictTypeById(Long dictId) {
        return this.getById(dictId);
    }

    @Override
    public CoolDictType selectDictTypeByType(String dictType) {
        return this.getOne(new LambdaQueryWrapper<CoolDictType>().eq(CoolDictType::getDictType, dictType));
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void deleteDictTypeByIds(Long[] dictIds) {
        for (Long dictId : dictIds) {
            CoolDictType dictType = selectDictTypeById(dictId);
            if (dictDataService.countDictDataByType(dictType.getDictType()) > 0) {
                throw new CoolServiceException(String.format("%1$s已分配,不能删除", dictType.getDictName()));
            }
            this.removeById(dictId);
        }
    }

    @Override
    public int insertDictType(CoolDictType dict) {
        if (checkDictTypeUnique(dict)) {
            throw new CoolServiceException("新增字典'" + dict.getDictName() + "'失败，字典类型已存在");
        }
        return this.save(dict) ? 1 : 0;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public int updateDictType(CoolDictType dict) {
        if (checkDictTypeUnique(dict)) {
            throw new CoolServiceException("修改字典'" + dict.getDictName() + "'失败，字典类型已存在");
        }
        CoolDictType oldDict = this.getById(dict.getId());
        dictDataService.updateDictDataType(oldDict.getDictType(), dict.getDictType());
        return this.updateById(dict) ? 1 : 0;
    }

    @Override
    public boolean checkDictTypeUnique(CoolDictType dict) {
        long dictId = Objects.isNull(dict.getId()) ? -1L : dict.getId();
        CoolDictType dictType = this.getOne(new LambdaQueryWrapper<CoolDictType>()
                .eq(CoolDictType::getDictType, dict.getDictType())
                .last("limit 1"));
        return dictType != null && dictType.getId() != dictId;
    }
}